package 新增道路查询后的最短距离II;

import java.util.HashMap;
import java.util.HashSet;

public class Solution {
    public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
        HashSet<Integer> set = new HashSet<>();
        HashMap<Integer, Integer> map = new HashMap<>();
        int[] res = new int[queries.length];
        for (int i = 0; i < n + 1; i++) {
            set.add(i);
        }
        for (int i = 0; i < queries.length; i++) {
            if(!set.contains(queries[i][0])) {
                res[i] = set.size() - 2;
                continue;
            }
            for (int j = queries[i][0] + 1; j < queries[i][1]; j++) {
                if(map.get(j) != null) {
                    j = map.get(j);
                }
                if(j >= queries[i][1]) {
                    break;
                }
                set.remove(j);
            }
            map.put(queries[i][0] + 1, queries[i][1]);
            res[i] = set.size() - 2;
        }
        return res;
    }
}